package org.xcorpio.java8.chapter05;

import java.util.Optional;

import org.xcorpio.java8.common.Data;
import org.xcorpio.java8.common.Dish;

public class FindMatching {

    public static void main(String[] args) {
        if (Data.Menu.stream().anyMatch(Dish::isVegetarian)) {
            System.out.println("The menu is (somewhat) vegetarian friendly!!");
        }
        boolean isHealthy = Data.Menu.stream()
                                     .allMatch(d -> d.getCalories() < 1000);
        System.out.println(isHealthy);
        System.out.println(Data.Menu.stream().noneMatch(d -> d.getCalories() >= 1000));
        /**
         * find an element
         * findAny: 在parallelStream时返回结果不确定
         * findFirst: 在parallelStream时也能返回第一个
         */
        Optional<Dish> any = Data.Menu.parallelStream()
                 .filter(Dish::isVegetarian)
                 .findAny();
        any.ifPresent(d -> System.out.println("findAny:" + d.getName()));
        Data.Menu.parallelStream().filter(Dish::isVegetarian).findFirst().ifPresent(System.out::println);
    }
}
